package com.android.settings.audio.peq.peq.ui;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;

import com.android.settings.R;
import com.android.settings.audio.peq.peq.QrTemplate;
import com.android.settings.audio.peq.qr.QrManager;
import com.android.settings.audio.peq.skin.SkinManager;
import com.android.settings.audio.peq.util.SmartPlayerApplication;
import com.android.settings.audio.peq.util.ToastTool;

/**
 * Created by We on 2023/6/2
 */
public class QrcodeActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.peq_activity_qrcode);
        handleIntent();
        init();

        //setStatusBarHeight(findViewById(R.id.rl_qr));
    }

    private void init() {
        if (TextUtils.isEmpty(qrId)) {
            finish();
            return;
        }
        if (qrContent == null) {
            finish();
            return;
        }
        if (qrLogoResId == -1) {
            finish();
            return;
        }
        QrTemplate.getInstance().init(this, findViewById(R.id.rl_qr), qrLogoResId, qrTypeTitle, value -> {
            System.out.println(value);
            return true;
        });

        android.graphics.Bitmap qrBitmap;
        try {
            qrBitmap = QrManager.getInstance().createQrcodeByEncoder(qrId, qrContent, 300, 300, BitmapFactory.decodeResource(getResources(), qrLogoResId));
        } catch (QrManager.BusinessException e) {
            ToastTool.showToast(SmartPlayerApplication.getInstance(), e.getLocalizedMessage());
            finish();
            return;
        }
        QrTemplate.getInstance().showQrcode(findViewById(R.id.rl_qr), qrBitmap);
        //PEQ的调音\n配合xx耳机食用更佳
        QrTemplate.getInstance().setQrcodeTitle(findViewById(R.id.rl_qr), qrTitle);
        android.view.View vCancel = findViewById(R.id.ll_cancel);
        if (vCancel != null) vCancel.setOnClickListener(v -> finish());
        android.view.View tvShare = findViewById(R.id.tv_share_cancel);
        if (tvShare != null) tvShare.setOnClickListener(v -> finish());

        if (tvShare != null && getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            //SkinManager.get().setButtonBackgroundSupportCustom(tvShare, R.drawable.skin_button_background_selector_5dp);
        }
        android.widget.TextView tvShareVia = findViewById(R.id.tvShareVia);
        if (null != tvShareVia) tvShareVia.setText("Share via " + Build.MODEL);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        QrTemplate.getInstance().onRequestPermissionResult(this, findViewById(R.id.rl_qr), requestCode, permissions, grantResults);
    }

    private String qrId;
    private byte[] qrContent;
    private int qrLogoResId;
    private String qrTitle;
    private String qrTypeTitle;

    private static final String QR_ID_NAME = "qrId";
    private static final String QR_CONTENT_NAME = "qrContent";
    private static final String QR_LOGO_NAME = "qrLogo";
    private static final String QR_TITLE = "qrTitle";
    private static final String QR_TYPE_TITLE = "qrTypeTitle";

    public static void start(Context context, String qrId, byte[] qrContent, @IdRes int qrLogoResId, String qrTitle, String qrTypeTitle) {
        Intent it = new Intent(context, QrcodeActivity.class);
        it.putExtra(QR_ID_NAME, qrId);
        it.putExtra(QR_CONTENT_NAME, qrContent);
        it.putExtra(QR_LOGO_NAME, qrLogoResId);
        it.putExtra(QR_TITLE, qrTitle);
        it.putExtra(QR_TYPE_TITLE, qrTypeTitle);
        context.startActivity(it);
    }

    private void handleIntent() {
        Intent intent = getIntent();
        if (null == intent) return;
        qrId = intent.getStringExtra(QR_ID_NAME);
        qrContent = intent.getByteArrayExtra(QR_CONTENT_NAME);
        qrLogoResId = intent.getIntExtra(QR_LOGO_NAME, -1);
        qrTitle = intent.getStringExtra(QR_TITLE);
        qrTypeTitle = intent.getStringExtra(QR_TYPE_TITLE);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        recreate();
    }
}
